package com.example.administrator.newjiajiesong.test;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.example.administrator.newjiajiesong.R;

import java.util.List;

/**
 * Created by Administrator on 2016/12/26.
 * <p>
 * 作者：xudiwei
 * <p>
 * 描述：RecyclerView的BaseAdapter
 */

public abstract class BaseRecyclerViewAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    /*普通的item类型*/
    protected static final int ITEM_TYPE_NORMAL = 0;
    /*加载更多的item类型*/
    protected static final int ITEM_TYPE_LOAD_MORE = -1;
    private static final String TAG = "BaseRecyclerViewAdapter";
    /*每页数据条数*/
    public int limit = 10;
    protected Context mContext;
    protected List<T> mList;
    /*item点击监听器*/
    protected OnItemClickListener mListener;
    /*是否还有更多数据可加载*/
    private boolean hasLoadMore = true;


    public BaseRecyclerViewAdapter(Context context, List<T> list) {
        this.mContext = context;
        this.mList = list;

    }

    /**
     * 设置每页条数
     *
     * @param limit
     */
    public void setLimit(int limit) {
        this.limit = limit;
    }


    /**
     * 更新数据
     *
     * @param list
     * @deprecated
     */
    public void upData(List<T> list) {
        this.mList = list;
        notifyDataSetChanged();
    }

    /**
     * 追加数据并更新
     *
     * @param list
     * @deprecated
     */
    public void upDataAdd(List<T> list) {
        if (list != null) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_LOAD_MORE) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_load_more, parent, false);
            return new LoadMoreViewHolder(view);
        } else {
            return onCreateViewHolderNormalItem(parent, viewType);
        }

//        if (viewType == ITEM_TYPE_NORMAL) {
//            return onCreateViewHolderNormalItem(parent, viewType);
//        } else {
//            View view = LayoutInflater.from(mContext).inflate(R.layout.item_load_more, parent, false);
//            return new LoadMoreViewHolder(view);
//        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position == mList.size()) {
            //bind load more view
            LoadMoreViewHolder loadMoreViewHolder = ((LoadMoreViewHolder) holder);
            if (hasLoadMore) {
                loadMoreViewHolder.tvLoadMore.setText("sdfds");
            } else {
                loadMoreViewHolder.tvLoadMore.setText("sdfds");
            }
        } else {
            //bind normal view
            onBindViewHolderNormalItem(holder, position);
        }
    }

    /**
     * 填充普通的item数据
     *
     * @param holder
     * @param position
     */
    public abstract void onBindViewHolderNormalItem(RecyclerView.ViewHolder holder, int position);

    /**
     * 创建普通的item view holder
     *
     * @param parent
     * @param viewType
     * @return
     */
    public abstract RecyclerView.ViewHolder onCreateViewHolderNormalItem(ViewGroup parent, int viewType);

    @Override
    public int getItemCount() {
        //只有集合不为空的情况下才默认添加最后一条加载更多的布局item
        if (mList == null) {
            return 0;
        } else {
            //数据小于limit条时不显示加载更多
            if (mList.size() < limit) {
                return mList.size();
            } else {
                return mList.size() + 1;
            }
        }
    }


    @Override
    public int getItemViewType(int position) {
        if (position == mList.size()) {
            return ITEM_TYPE_LOAD_MORE;
        }
        return ITEM_TYPE_NORMAL;
    }

    /**
     * 通知数据改变，通过调用此方法实现数据改变刷新
     *
     * @param hasLoadMore 是否还有更多数据加载，true表示为有更多数据
     *                    这时滑到最后一条item显示的是“加载更多中” 当为false
     *                    表示没有更多数据了。这时滑到最后一条item.显示“没有更多数据了”
     */
    public void myNotifyDataSetChanged(boolean hasLoadMore) {
        this.hasLoadMore = hasLoadMore;
        this.notifyDataSetChanged();
    }

    /**
     * ItemView点击
     *
     * @param normalItemView
     * @param viewHolder
     */
    protected void setItemClick(final View normalItemView, final RecyclerView.ViewHolder viewHolder) {
        if (null != mListener) {
            normalItemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(normalItemView, viewHolder.getLayoutPosition());
                }
            });
        }
    }

    /**
     * 设置item点击事件监听器
     *
     * @param listener
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    /**
     * item点击事件回调接口{@link #setOnItemClickListener(OnItemClickListener)}
     */
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    /**
     * 加载更多的ViewHolder
     */
    private class LoadMoreViewHolder extends RecyclerView.ViewHolder {
        TextView tvLoadMore;

        public LoadMoreViewHolder(View itemView) {
            super(itemView);
            tvLoadMore = (TextView) itemView.findViewById(R.id.tv_loadmore);
        }
    }


}
